package com.zcxy.fast.mybatisExtend;


import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
import lombok.Data;
import org.apache.ibatis.reflection.property.PropertyNamer;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zcc
 * @version 1.0.0
 * @ClassName LambdaValiteCondition
 * @Description TODO
 * @createTime 2020年02月22日 15:12:00
 */
public class LambdaValiteCondition<T> {

    private Map<String, Object> filedConsumer = new HashMap();
    private Map<String, Object> excludeConsumer = new HashMap();
    private String errInfo = "存在重复属性,添加或者修改失败";
    T t = null;

    /***
     * 需要校验重复的字段
     * @param action
     * @return
     */
    public LambdaValiteCondition addField(SFunction<T, Object>... action) {
        for (SFunction ac : action) {
            Object r = ac.apply(t);
            SerializedLambda serializedLambda = LambdaUtils.resolve(ac);
            String fieldName = PropertyNamer.methodToProperty(serializedLambda.getImplMethodName());
            filedConsumer.put(fieldName, r);
        }
        return this;
    }

    /**
     * 校验重复需要排除的字段
     *
     * @param action
     * @return
     */
    public LambdaValiteCondition addExcludes(SFunction<T, Object>... action) {
        for (SFunction ac : action) {
            Object r = ac.apply(t);
            SerializedLambda serializedLambda = LambdaUtils.resolve(ac);
            String fieldName = PropertyNamer.methodToProperty(serializedLambda.getImplMethodName());
            excludeConsumer.put(fieldName, r);
        }
        return this;
    }

    public LambdaValiteCondition addErrInfo(String value) {
        this.errInfo = value;
        return this;
    }

    private LambdaValiteCondition(T t) {
        this.t = t;
    }

    public static <T> LambdaValiteCondition newInstance(T t) {
        return new LambdaValiteCondition(t);
    }
    public   <T> LambdaValiteCondition clear() {
        this.filedConsumer=new HashMap<>();
        this.excludeConsumer=new HashMap<>();
        this.errInfo="存在重复属性,添加或者修改失败";
        return this;
    }

    public Map<String, Object> getFiledConsumer() {
        return filedConsumer;
    }


    public String getErrInfo() {
        return errInfo;
    }

    public void setErrInfo(String errInfo) {
        this.errInfo = errInfo;
    }

    public Map<String, Object> getExcludeConsumer() {
        return excludeConsumer;
    }
}
